/**
 * Copyright (c) 2020 NumberFour AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   NumberFour AG - Initial API and implementation
 */
package org.eclipse.n4js.ide.tests.builder;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.eclipse.n4js.workspace.locations.FileURI;
import org.junit.Test;

/**
 * Test two cases of projects with the same project name within a yarn setup
 */

public class YarnDifferentPackageNamesTest extends AbstractIncrementalBuilderTest {

	private static Map<String, Map<String, String>> testData = Map.of(
			"main-project", Map.of(
					"MainModule", """
								import {LibraryClass} from "LibraryModule";
								import {LibraryVar} from "library-project-other-name/LibraryModule";

								new LibraryClass();
								LibraryVar;
							""",
					CFG_DEPENDENCIES, """
								library-project-other-name
							"""),
			"library-project", Map.of(
					"LibraryModule", """
								export public class LibraryClass {
								}
								export public const LibraryVar = 0;
							""",
					PACKAGE_JSON, """
								{
									"name": "library-project-other-name",
									"version": "0.0.1",
									"dependencies": {
										"n4js-runtime": "*"
									},
									"n4js": {
										"projectType": "library",
										"mainModule": "LibraryModule",
										"vendorId": "org.eclipse.n4js",
										"sources": {
											"source": [
												"src"
											]
										},
										"output": "src-gen"
									}
								}
							"""));

	@Test
	public void twoEqualProjectNames() throws Exception {
		testWorkspaceManager.createTestOnDisk(testData);

		startAndWaitForLspServer();
		assertIssues2(Map.of("library-project/package.json", List.of(
				"(Warning, [1:10 - 1:38], As a convention the package name 'library-project-other-name' should match the name of the project folder 'library-project' on the file system.)")));

		assertOutputFileExists("main-project", "MainModule");
		assertOutputFileExists("library-project", "LibraryModule");

		File outputFileMain = getOutputFile("main-project", "MainModule");
		File outputFileLibrary = getOutputFile("library-project", "LibraryModule");

		// assert correct import module
		assertContentOfFileOnDisk(new FileURI(outputFileMain), """
				// Generated by N4JS transpiler; for copyright see original N4JS source file.

				import 'n4js-runtime'
				import {LibraryClass} from 'library-project-other-name'
				import {LibraryVar} from 'library-project-other-name'

				new LibraryClass();
				LibraryVar;
				//# sourceMappingURL=MainModule.map
				""");

		// assert correct package name in reflection
		assertContentOfFileOnDisk(new FileURI(outputFileLibrary),
				"""
						// Generated by N4JS transpiler; for copyright see original N4JS source file.

						import 'n4js-runtime'

						export class LibraryClass extends N4Object {
							static get n4type() {
								return $getReflectionForClass(this, '["LibraryClass","LibraryModule","library-project-other-name"]');
							}
						}
						export const LibraryVar = 0;
						//# sourceMappingURL=LibraryModule.map
						""");
	}

}
